﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyCommerce.ManagementBase.Common.Base.Common;
using MyCommerce.Presentation.Stores.Presenters;
using MyCommerce.Presentation.Stores.IViews;
using Innova.Microfinanzas.BusinessEntities;

namespace MyCommerce.Windows.Stores.Views
{
    public partial class ProductStockView : BaseForm, IProductStock
    {
        private ProductStockPresenter vPresenter;
        private bool isUpdate = false;

        public ProductStockView()
        {
            InitializeComponent();
            vPresenter = new ProductStockPresenter((IProductStock)this);
        }

        #region IStore Members

        private ProductStock vobjProductStock;
        public ProductStock objProductStock
        {
            get
            {
                vobjProductStock = new ProductStock();
                if (isUpdate)
                {
                    //vobjProductStock.IdProductStock = (long)gridStore.ActiveRow.Cells["Id"].Value;
                }
                vobjProductStock.IdProduct = (long)cmbProduct.SelectedRow.Cells[""].Value;
                vobjProductStock.IdSupplier = (long)cmbSupplier.SelectedValue;
                vobjProductStock.IdStore = (long)cmbStore.SelectedValue;
                vobjProductStock.SerialNumber = txtSerialNumber.Text;
                vobjProductStock.IdProductPrice = (long)cmbProductPrice.SelectedRow.Cells[""].Value;
                return vobjProductStock;
            }
            set
            {
                
            }
        }


        public new event EventHandler OnLoad;

        public event OnSaveEventHandler OnSave;

        public event OnEntityEventHandler OnEdit;

        public event OnEntityEventHandler OnDelete;

        #endregion

        private void StoreView_Load(object sender, EventArgs e)
        {
            OnLoad(sender, e);
            Modify(false);
        }

        private void tsNewButtonToolStrip_Click(object sender, EventArgs e)
        {
            commonButtonsToolStrip1.ToggleButtons();
            Modify(true);
        }

        private void tsEditButtonToolStrip_Click(object sender, EventArgs e)
        {
            OnEdit(sender, (long)gridStore.ActiveRow.Cells["IdConveyor"].Value);
            isUpdate = true;

            commonButtonsToolStrip1.ToggleButtons();
            Modify(true);
        }

        private void tsDeleteButtonToolStrip_Click(object sender, EventArgs e)
        {
            if (Context.ShowQuestionMessageBox("¿Desea eliminar a este parametro?", "Eliminar parametro") == System.Windows.Forms.DialogResult.Yes)
            {
                OnDelete(sender, (long)gridStore.ActiveRow.Cells["IdConveyor"].Value);
                Context.ShowInformationMessageBox("El parametro se eliminó correctamente.", "Eliminar parametros");
            }   
        }

        private void tsSaveButtonToolStrip_Click(object sender, EventArgs e)
        {
            if (DataAutoValidate())
            {
                if (!isUpdate)
                {
                    if (OnSave != null)
                    {
                        OnSave(this, true);
                        Context.ShowInformationMessageBox("La información se guardó correctamente.", "Creación/Edición de usuarios.");
                    }

                }
                else
                {
                    if (OnSave != null)
                    {
                        OnSave(this, false);
                        Context.ShowInformationMessageBox("La información se actualizó correctamente.", "Creación/Edición de usuarios.");
                    }
                }
            }
            isUpdate = false;
            commonButtonsToolStrip1.ToggleButtons();
            Modify(false);
            CleanScreen();
        }

        private void tsCancelButtonToolStrip_Click(object sender, EventArgs e)
        {
            isUpdate = false;
            commonButtonsToolStrip1.ToggleButtons();
            Modify(false);
            CleanScreen();
        }

        private void tsCloseButtonToolStrip_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private Boolean DataAutoValidate()
        {
            bool result = true;
            StringBuilder message = new StringBuilder();

            if (string.IsNullOrEmpty(txtSerialNumber.Text.Trim()))
            {
                result = false;
                message.AppendLine("Debe ingresar un nombre.");
            }

            if (!result)
            {
                Context.ShowErrorMessageBox(message.ToString(), "Validación de datos.");
            }

            return result;
        }

        private void Modify(bool edit)
        {
            gridStore.Enabled = !edit;
            txtSerialNumber.Enabled = edit;
        }

        private void CleanScreen()
        {
            txtSerialNumber.Text = string.Empty;
        }


        #region IProductStock Members


        public List<Product> colProduct
        {
            set 
            {
                cmbProduct.ValueMember = "IdProduct";
                cmbProduct.DisplayMember = "Model";
                cmbProduct.DataSource = value;
            }
        }

        public List<ProductPrice> colProductPrice
        {
            set
            {
                cmbProductPrice.ValueMember = "IdProductPrice";
                cmbProductPrice.DisplayMember = "SalePrice";
                cmbProductPrice.DataSource = value;
            }
        }

        public List<Store> colStore
        {
            set
            {
                cmbStore.ValueMember = "IdStore";
                cmbStore.DisplayMember = "Name";
                cmbStore.DataSource = value;
            }
        }

        public List<Supplier> colSupplier
        {
            set
            {
                cmbSupplier.ValueMember = "IdSupplier";
                cmbSupplier.DisplayMember = "Name";
                cmbSupplier.DataSource = value;
            }
        }

        #endregion
    }
}
